﻿@using eLoan.Models;
@model LoanDeductionAddEditModel

@{
    ViewBag.Title = "Add/Edit Loan Deductions";

    var documentNo = ViewBag.DocumentNo;

}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Add/Edit Loan Deductions</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li>
                <a href="@Url.Action("Loans", "Funding")">Loans</a>
            </li>
            <li>
                <a href="@Url.Action("LoanDetails", "Loan", new { documentNo = documentNo })">Loan Details</a>
            </li>
            <li class="active">
                <strong>Add/Edit Loan Deductions</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-6">
            <div class="ibox float-e-margins">
                <div class="ibox-content">

                    @using (Html.BeginForm("Save", "LoanDeduction", FormMethod.Post, new { id = "saveForm" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <fieldset>

                            <table class="table table-striped table-bordered">
                                <thead class="k-grid-header">
                                    <tr>
                                        <th class="k-header">Charge Type</th>
                                        <th class="k-header">Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @for (var i = 0; i < Model.LoanDeductions.Count(); i++)
                                    { 
                                        <tr>
                                            <td>
                                                @Html.HiddenFor(m => Model.DBCommand)
                                                @Html.DisplayFor(m => Model.LoanDeductions[i].ChargeTypeName)
                                                @Html.HiddenFor(m => Model.LoanDeductions[i].ChargeTypeName)
                                                @Html.HiddenFor(m => Model.LoanDeductions[i].LoanDeduction.ID)
                                                @Html.HiddenFor(m => Model.LoanDeductions[i].LoanDeduction.ChargeTypeID)
                                                @Html.HiddenFor(m => Model.LoanDeductions[i].LoanDeduction.DocumentNo)
                                            </td>
                                            <td>@Html.TextBoxFor(m => Model.LoanDeductions[i].LoanDeduction.Amount, "{0:#,###,##0.00}" , new { @class= "single-line scroll-key-navigate", @onchange = "FormatMe(this);" })</td>
                                        </tr>
                                    }
                                </tbody>
                            </table>

                            <p></p>

                            <button type="submit" id="btnSave" class="btn btn-primary" name="afterSave" value="@Model.DBCommand,@documentNo">Save</button>
                            <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="afterSave" value="Cancel,@documentNo">Cancel</button>

                        </fieldset>


                    }

                </div>
            </div>

        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">

        $(document).ready(function () {

            var collection = $('.scroll-key-navigate');

            collection.each(function (i, item) {

                $(item).keyup(function (e) {
                    e = e || window.event;
                    
                    switch (e.keyCode)
                    {
                        case 37:
                            {
                                break;
                            }
                        case 38:
                            {
                                var nextIndex = i != 0 ? i - 1 : collection.length - 1;
                                $(collection[nextIndex]).focus();
                                break;
                            }
                        case 39:
                            {
                                break;
                            }
                        case 40:
                            {
                                var nextIndex = i != (collection.length - 1) ? i + 1 : 0;
                                $(collection[nextIndex]).focus();
                                break;
                            }
                    }

                });

            });

        });

        function FormatMe(me) {
            var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

            if (isNaN(parseFloat(val)))
                val = 0.00;

            var decimalFormat = parseFloat(val).toFixed(2);
            var commaFormat = FormatNumber(decimalFormat);

            $(me).val(commaFormat);
        }

        function FormatNumber(nStr) {
            nStr += '';
            var x = nStr.split('.');
            var x1 = x[0];
            var x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }
    </script>
}


